﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.SoldierArchives.Model;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;
using Yipusoft.Common.BLL;
using Yipusoft.TranslateApprove.BLL;
using Yipusoft.TranslateApprove.Model;
using Yipusoft.TranslateApprove.DAL;

public partial class Police_Select_SelectInner : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            UserInfo member = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
            //内部调动部门
            DepartmentList.DataSource = GetDepartmentList(member.DepartmentID, "203001", "203002", "203003");
            DepartmentList.DataTextField = "FullName";
            DepartmentList.DataValueField = "DepartmentID";
            DepartmentList.DataBind();

            if (!string.IsNullOrEmpty(Request["ID"]))
            {

                string sid = Request["ID"];
                Soldier soldier = new Soldier();
                SoldierArchiveInfo soldierInfo = soldier.GetSoldierInfoByID(sid);
                SelectDataContext selectDB = new SelectDataContext();
                if (selectDB.TranslateSelect.Where(o => o.SoldierId == new Guid(sid)).Count() > 0)
                {
                    Response.Write("<script>alert('此士兵已存在于审核人员列表中！');history.back(0);</script>");
                    return;

                }
                if (soldierInfo.IsContract)
                {
                    Caption.Text = "合同制队员基本情况";
                    lblRank.Visible = false;
                    RankName.Visible = false;
                    lblCardID.Text = "编号：";
                    lblDateJoin.Text = "工作时间：";
                }
                else
                {
                    Caption.Text = "现役士兵基本情况";
                    lblCardID.Text = "士兵证号：";
                    lblDateJoin.Text = "入伍时间：";
                }
                BindSoldier(soldierInfo);

            }
            if (!string.IsNullOrEmpty(Request["SelectId"]))
            {
                SelectDataContext db = new SelectDataContext();
                var Select = db.TranslateSelect.Where(o => o.Id == new Guid(Request["SelectId"])).FirstOrDefault();
                DepartmentList.SelectedValue = Select.TanslateDepartmentId;
                LinkMen.Text = Select.LinkMen;
                ApproveMen.Text = Select.AproveMen;
            }

        }

    }

    public string GetSoldierID()
    {
        return Request.QueryString["ID"];
    }

    private void BindSoldier(SoldierArchiveInfo soldierInfo)
    {

        SoldierName.Text = soldierInfo.SoldierName;
        CardID.Text = soldierInfo.CardID;
        Sex.Text = soldierInfo.Sex;
        imgPhoto.ImageUrl = soldierInfo.Picture == "" ? "~/Images/NoPhoto.png" : "../../" + soldierInfo.Picture;
        DepartmentName.Text = soldierInfo.DepartmentName;
        NationName.Text = soldierInfo.NativeName;
        Birthday.Text = soldierInfo.Birthday.Year == 9999 ? "--" : soldierInfo.Birthday.ToString("yyyy-MM-dd");
        JoinDate.Text = soldierInfo.JoinTime.Year == 9999 ? "--" : soldierInfo.JoinTime.ToString("yyyy-MM-dd");
        RankName.Text = soldierInfo.RankName;
        DutyName.Text = soldierInfo.DutyName;
        SpecialityName.Text = soldierInfo.Speciality2 == "" ? soldierInfo.Speciality1Name : soldierInfo.Speciality1Name + "[" + soldierInfo.Speciality2Name + "]";
    }



    private List<DepartmentInfo> GetDepartmentList(string departmentID, string permissionAdmin, string permissionLevel, string permissionUnit)
    {
        List<DepartmentInfo> departmentList = new List<DepartmentInfo>();
        if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionAdmin))
            departmentList = Department.GetList("(SUBSTRING(DepartmentID, 5, 3) = '000') OR (DepartmentID = '1313100') OR (DepartmentID = '1312100') or (DepartmentID = '1300017') or (DepartmentID = '1300038')or (DepartmentID = '1300035')");
        else

            if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionLevel))
                departmentList = Department.GetList("DepartmentID like '" + departmentID.Substring(0, 4) + "%'");
            else
                if (Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, permissionUnit))
                    departmentList = Department.GetList("DepartmentID='" + departmentID + "'");

        return departmentList;
    }

    public List<DepartmentInfo> GetDepartmentList()
    {
        List<DepartmentInfo> departmentList = new List<DepartmentInfo>();
        departmentList = Department.GetList("Right(DepartmentID,2)='00'");
        return departmentList;
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        string sid = Request["ID"];
        Soldier soldier = new Soldier();
        SoldierArchiveInfo soldierInfo = soldier.GetSoldierInfoByID(sid);
        if (Request.QueryString["SelectId"] != null)
        {
            try
            {
                SelectDataContext db = new SelectDataContext();
                TranslateSelect select = db.TranslateSelect.Where(o => o.Id == new Guid(Request.QueryString["SelectId"])).FirstOrDefault();
                select.JoinTime = soldierInfo.JoinTime.Year == 1 ? DateTime.MaxValue : soldierInfo.JoinTime; ;
                select.LastDate = DateTime.Now;
                select.OldDepartment = soldierInfo.DepartmentName;
                select.Rank = soldierInfo.Rank;
                select.Sex = Sex.Text;
                select.SoldierName = SoldierName.Text;
                select.TanslateDepartment = DepartmentList.SelectedItem.Text;
                select.TanslateDepartment = DepartmentList.SelectedItem.Text;
                select.TanslateDepartmentId = DepartmentList.SelectedValue;
                select.OldDepartment = soldierInfo.DepartmentName;
                select.OldDepartment2 = soldierInfo.DepartmentName;
                select.OldDepartmentId = soldierInfo.DepartmentID;
                select.FromJingZhong = "SBDD30";
                select.TanslateType = 2;
                select.ToJingZhong = "SBDD30";
                select.SoldierId = new Guid(sid);
                select.AproveMen = ApproveMen.Text;
                select.LinkMen = LinkMen.Text;
                db.SubmitChanges();
                Prompt.Text = "修改成功！";
                // Response.Redirect("SelectList.aspx");
            }
            catch (Exception ex)
            {
                Prompt.Text = ex.Message;
            }
        }
        else
        {
            try
            {
                SelectDataContext db = new SelectDataContext();
                TranslateSelect select = new TranslateSelect();
                select.Id = Guid.NewGuid();
                select.JoinTime = soldierInfo.JoinTime.Year == 1 ? DateTime.MaxValue : soldierInfo.JoinTime; ;
                select.LastDate = DateTime.Now;
                select.OldDepartment = soldierInfo.DepartmentName;
                select.Rank = soldierInfo.Rank;
                select.Sex = Sex.Text;
                select.SoldierName = SoldierName.Text;
                select.TanslateDepartment = DepartmentList.SelectedItem.Text;
                select.TanslateDepartment = DepartmentList.SelectedItem.Text;
                select.TanslateDepartmentId = DepartmentList.SelectedValue;
                select.OldDepartment = soldierInfo.DepartmentName;
                select.OldDepartment2 = soldierInfo.DepartmentName;
                select.OldDepartmentId = soldierInfo.DepartmentID;
                select.FromJingZhong = "SBDD30";
                select.TanslateType = 2;
                select.ToJingZhong = "SBDD30";
                select.SoldierId = new Guid(sid);
                select.AproveMen = ApproveMen.Text;
                select.LinkMen = LinkMen.Text;
                db.TranslateSelect.InsertOnSubmit(select);
                db.SubmitChanges();
                Prompt.Text = "添加成功！";
                // Response.Redirect("SelectList.aspx");
            }
            catch
            {
                Prompt.Text = "添加失败！";
            }
        }

    }
    protected void btnCancal_Click(object sender, EventArgs e)
    {
        if (Request["SelectId"] != null)
        {
            Response.Redirect("SelectList.aspx");
        }
        else
        {
            Response.Redirect("../TranslateProcess/SoldierSelect.aspx?T=3");
        }
    }
}
